package cn.zzdt4j.config.service.biz.impl;

import cn.hutool.core.util.StrUtil;
import cn.zzdt4j.common.toolkit.BeanUtil;
import cn.zzdt4j.config.mapper.OperationLogMapper;
import cn.zzdt4j.config.model.LogRecordInfo;
import cn.zzdt4j.config.model.biz.log.LogRecordQueryReqDTO;
import cn.zzdt4j.config.model.biz.log.LogRecordRespDTO;
import cn.zzdt4j.config.service.biz.OperationLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * The type Operation log service.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023 /11/8 16:09
 */
@Service
@AllArgsConstructor
public class OperationLogServiceImpl implements OperationLogService {

    private final OperationLogMapper operationLogMapper;

    @Override
    @SuppressWarnings("unchecked")
    public IPage<LogRecordRespDTO> queryPage(LogRecordQueryReqDTO reqDTO) {
        final LambdaQueryWrapper<LogRecordInfo> queryWrapper = Wrappers.lambdaQuery(LogRecordInfo.class)
                .eq(StrUtil.isNotBlank(reqDTO.getBizNo()), LogRecordInfo::getBizNo, reqDTO.getBizNo())
                .eq(StrUtil.isNotBlank(reqDTO.getCategory()), LogRecordInfo::getCategory, reqDTO.getCategory())
                .eq(StrUtil.isNotBlank(reqDTO.getOperator()), LogRecordInfo::getOperator, reqDTO.getOperator())
                .orderByDesc(LogRecordInfo::getCreateTime);
        final LogRecordQueryReqDTO selectPage = operationLogMapper.selectPage(reqDTO, queryWrapper);
        return selectPage.convert(each -> BeanUtil.convert(each, LogRecordRespDTO.class));
    }

    @Override
    public void record(LogRecordInfo logRecordInfo) {
        operationLogMapper.insert(logRecordInfo);
    }
}
